package com.ztjy.purchase.controller;

import com.github.pagehelper.PageInfo;
import com.ztjy.annonation.MyLog;
import com.ztjy.bean.CDetails;
import com.ztjy.bean.CPurchase;
import com.ztjy.bean.Users;
import com.ztjy.purchase.biz.CDetailsBiz;
import com.ztjy.purchase.biz.CPurchaseBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.List;

@Controller
@RequestMapping("purchase/purchaseExamine")
public class CDetailsController {
    @Autowired
    private CPurchaseBiz cbiz;
    @Autowired
    private CDetailsBiz cdbiz;


    //每次执行前会优先 将mid放入request范围
    @ModelAttribute
    public void initModuleData(HttpSession session) {
        session.removeAttribute("mid");
        session.setAttribute("mid", 16);
    }

    @MyLog(value = "查询所有采购单审核操作")
    @RequestMapping("/dlist")
    public String All(
            @RequestParam(value = "pageno",defaultValue = "1")Integer pageNo,
            CPurchase cPurchase,Model model,String mintime, String maxtime,
            @RequestParam(value = "mintotal",required=false) Double mintotal,
            @RequestParam(value = "maxtotal",required=false)Double maxtotal){

        Date minTime=null;
        if (mintime!=null && !mintime.equals("")){
            minTime=Date.valueOf(mintime);
        }
        Date maxTime=null;
        if (maxtime!=null && !maxtime.equals("")){
            maxTime=Date.valueOf(maxtime);
        }

        Integer pageSize=6;
        PageInfo<CPurchase> cpb = cbiz.findCPurchaseByCountStatus2(pageNo, pageSize, cPurchase, minTime, maxTime, mintotal, maxtotal);
        model.addAttribute("cpb",cpb);
        model.addAttribute("minTime",minTime);
        model.addAttribute("maxTime",maxTime);
        model.addAttribute("mintotal",mintotal);
        model.addAttribute("maxtotal",maxtotal);
        model.addAttribute("pur",cPurchase);
        List<CDetails> all = cdbiz.findAll();
        model.addAttribute("cdeta",all);
        return "forward:/purchase/purchaseExamine/purchaseExamineList.html";
    }
    @MyLog(value = "详情操作")
        @RequestMapping("/dateils/{id}")
    public String de(@PathVariable("id")String purchaseId,Model model){
            CPurchase cPurchaseByPid = cbiz.findCPurchaseByPid(purchaseId);
            List<CDetails> cDetails = cdbiz.findByIdCDetails(purchaseId);
            model.addAttribute("cpdate",cPurchaseByPid);
            model.addAttribute("cdate",cDetails);
            return "forward:/purchase/purchaseExamine/purchaseExamine";
        }
    @MyLog(value = "修改操作")
        @RequestMapping("/dedit")
        public String edit(CPurchase purchase,HttpSession session){
            purchase.setCheckTime(new java.util.Date(System.currentTimeMillis()));
        Users currUser = (Users) session.getAttribute("currUser");
        purchase.setCheckId(currUser.getuId());
            boolean flag = cbiz.updateCPurchase(purchase);
            if(flag){
                return "redirect:/purchase/purchaseExamine/dlist";
            }
             return "";
        }
}
